<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\ArticleEditRequest;
use Cache;
use App\Models\Article;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
	/**
	 * 文章列表
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function lst() {
		$logmin = date('Y-m-d');
		$logmax = date('Y-m-d');
		return view('admin.article.list', compact('logmin', 'logmax'));
	}

	/**
	 * 文章列表
	 * @param Request $request
	 * @return array
	 */
	public function list(Request $request) {
		return (new Article())->list($request, 'title');
	}

	/**
	 * 删除单个
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function del(Article $article) {
		$res = $article->delete();
		Cache::delete('art' . $article->id);
		if ($res) {
			return response()->json(['status' => 0, 'msg' => '删除成功']);
		}
		return response()->json(['status' => 1, 'msg' => '删除失败'], 400);
	}

	/**
	 * 删除多个
	 * @param Request $request
	 * @return array
	 * @throws \Exception
	 */
	public function delMore(Request $request) {
		$idArr = $request->get('idArr');
		$res = (new Article())->whereIn('id', $idArr)->delete();
		if ($res) {
			return ['status' => 0, 'msg' => '删除成功'];
		}
		return ['status' => 1, 'msg' => '删除失败'];
	}

	/**
	 * 查看单个
	 * @param $id
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function one($id) {
		if (Cache::has('art' . $id)) {
			$art = Cache::get('art' . $id);
		} else {
			$art = Article::find($id);
			Cache::add('art' . $id, $art, 60);
		}
		return view('admin.article.art', compact('art'));
	}

	/**
	 * 编辑
	 * @param Article $article
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function edit(Article $article) {
		if (Cache::has('art' . $article->id)) {
			$art = Cache::get('art' . $article->id);
		} else {
			$art = Article::find($article->id);
			Cache::add('art' . $article->id, $art, 60);
		}
		return view('admin.article.edit', compact('art'));
	}

	/**
	 * 修改处理
	 * @param ArticleEditRequest $request
	 * @param int                $id
	 * @return array
	 */
	public function editSave(ArticleEditRequest $request, int $id) {
		$title = $request->get('title');
		$content = $request->get('content');
		$res = Article::where(['id' => $id])->update(compact('title', 'content'));
		if ($res) {
			Cache::put('art' . $id, Article::find(1), 60);
			return ['status' => 0, 'msg' => '成功'];
		} else {
			return ['status' => 1, 'msg' => '失败'];
		}
	}

	/**
	 * 添加文章页
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function add() {
		return view('admin.article.add');
	}

	/**
	 * 添加文章处理
	 * @param ArticleEditRequest $request
	 * @return array
	 */
	public function addSave(ArticleEditRequest $request) {
		$all = $request->all();
		$res = Article::create($all);
		if ($res) {
			Cache::add('res' . $res->id, $res, 60);
			return ['status' => 0, 'msg' => '成功'];
		} else {
			return ['status' => 1, 'msg' => '失败'];
		}
	}
}
